//
//  DeviceModelUtil.m
//  Peso Guide
//
//  Created by macbook on 2025/6/6.
//

#import "DeviceModelUtil.h"
#import <sys/utsname.h>

@implementation DeviceModelUtil

+ (NSString *)deviceModel {
    struct utsname systemInfo;
    uname(&systemInfo);
    return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}

+ (NSString *)deviceModelName {
    NSString *model = [self deviceModel];
    
    NSDictionary *deviceMapping = @{
        // iPhone
        @"iPhone1,1": @"iPhone",
        @"iPhone1,2": @"iPhone 3G",
        @"iPhone2,1": @"iPhone 3GS",
        @"iPhone3,1": @"iPhone 4",
        @"iPhone3,2": @"iPhone 4",
        @"iPhone3,3": @"iPhone 4",
        @"iPhone4,1": @"iPhone 4s",
        @"iPhone5,1": @"iPhone 5",
        @"iPhone5,2": @"iPhone 5",
        @"iPhone5,3": @"iPhone 5c",
        @"iPhone5,4": @"iPhone 5c",
        @"iPhone6,1": @"iPhone 5s",
        @"iPhone6,2": @"iPhone 5s",
        @"iPhone7,1": @"iPhone 6 Plus",
        @"iPhone7,2": @"iPhone 6",
        @"iPhone8,1": @"iPhone 6s",
        @"iPhone8,2": @"iPhone 6s Plus",
        @"iPhone8,4": @"iPhone SE (1st gen)",
        @"iPhone9,1": @"iPhone 7",
        @"iPhone9,3": @"iPhone 7",
        @"iPhone9,2": @"iPhone 7 Plus",
        @"iPhone9,4": @"iPhone 7 Plus",
        @"iPhone10,1": @"iPhone 8",
        @"iPhone10,4": @"iPhone 8",
        @"iPhone10,2": @"iPhone 8 Plus",
        @"iPhone10,5": @"iPhone 8 Plus",
        @"iPhone10,3": @"iPhone X",
        @"iPhone10,6": @"iPhone X",
        @"iPhone11,2": @"iPhone XS",
        @"iPhone11,4": @"iPhone XS Max",
        @"iPhone11,6": @"iPhone XS Max",
        @"iPhone11,8": @"iPhone XR",
        @"iPhone12,1": @"iPhone 11",
        @"iPhone12,3": @"iPhone 11 Pro",
        @"iPhone12,5": @"iPhone 11 Pro Max",
        @"iPhone12,8": @"iPhone SE (2nd gen)",
        @"iPhone13,1": @"iPhone 12 mini",
        @"iPhone13,2": @"iPhone 12",
        @"iPhone13,3": @"iPhone 12 Pro",
        @"iPhone13,4": @"iPhone 12 Pro Max",
        @"iPhone14,4": @"iPhone 13 mini",
        @"iPhone14,5": @"iPhone 13",
        @"iPhone14,2": @"iPhone 13 Pro",
        @"iPhone14,3": @"iPhone 13 Pro Max",
        @"iPhone14,6": @"iPhone SE (3rd gen)",
        @"iPhone14,7": @"iPhone 14",
        @"iPhone14,8": @"iPhone 14 Plus",
        @"iPhone15,2": @"iPhone 14 Pro",
        @"iPhone15,3": @"iPhone 14 Pro Max",
        @"iPhone15,4": @"iPhone 15",
        @"iPhone15,5": @"iPhone 15 Plus",
        @"iPhone16,1": @"iPhone 15 Pro",
        @"iPhone16,2": @"iPhone 15 Pro Max",
        
        // iPad
        @"iPad1,1": @"iPad",
        @"iPad2,1": @"iPad 2",
        @"iPad2,2": @"iPad 2",
        @"iPad2,3": @"iPad 2",
        @"iPad2,4": @"iPad 2",
        @"iPad3,1": @"iPad (3rd gen)",
        @"iPad3,2": @"iPad (3rd gen)",
        @"iPad3,3": @"iPad (3rd gen)",
        @"iPad3,4": @"iPad (4th gen)",
        @"iPad3,5": @"iPad (4th gen)",
        @"iPad3,6": @"iPad (4th gen)",
        @"iPad4,1": @"iPad Air",
        @"iPad4,2": @"iPad Air",
        @"iPad4,3": @"iPad Air",
        @"iPad5,3": @"iPad Air 2",
        @"iPad5,4": @"iPad Air 2",
        @"iPad6,11": @"iPad (5th gen)",
        @"iPad6,12": @"iPad (5th gen)",
        @"iPad7,5": @"iPad (6th gen)",
        @"iPad7,6": @"iPad (6th gen)",
        @"iPad7,11": @"iPad (7th gen)",
        @"iPad7,12": @"iPad (7th gen)",
        @"iPad11,6": @"iPad (8th gen)",
        @"iPad11,7": @"iPad (8th gen)",
        @"iPad12,1": @"iPad (9th gen)",
        @"iPad12,2": @"iPad (9th gen)",
        @"iPad13,18": @"iPad (10th gen)",
        @"iPad13,19": @"iPad (10th gen)",
        
        // iPad Pro
        @"iPad6,3": @"iPad Pro (9.7-inch)",
        @"iPad6,4": @"iPad Pro (9.7-inch)",
        @"iPad7,3": @"iPad Pro (10.5-inch)",
        @"iPad7,4": @"iPad Pro (10.5-inch)",
        @"iPad8,1": @"iPad Pro (11-inch)",
        @"iPad8,2": @"iPad Pro (11-inch)",
        @"iPad8,3": @"iPad Pro (11-inch)",
        @"iPad8,4": @"iPad Pro (11-inch)",
        @"iPad8,9": @"iPad Pro (11-inch) (3rd gen)",
        @"iPad8,10": @"iPad Pro (11-inch) (3rd gen)",
        @"iPad13,4": @"iPad Pro (11-inch) (4th gen)",
        @"iPad13,5": @"iPad Pro (11-inch) (4th gen)",
        @"iPad13,6": @"iPad Pro (11-inch) (4th gen)",
        @"iPad13,7": @"iPad Pro (11-inch) (4th gen)",
        
        // iPad mini
        @"iPad2,5": @"iPad mini",
        @"iPad2,6": @"iPad mini",
        @"iPad2,7": @"iPad mini",
        @"iPad4,4": @"iPad mini 2",
        @"iPad4,5": @"iPad mini 2",
        @"iPad4,6": @"iPad mini 2",
        @"iPad4,7": @"iPad mini 3",
        @"iPad4,8": @"iPad mini 3",
        @"iPad4,9": @"iPad mini 3",
        @"iPad5,1": @"iPad mini 4",
        @"iPad5,2": @"iPad mini 4",
        @"iPad11,1": @"iPad mini (5th gen)",
        @"iPad11,2": @"iPad mini (5th gen)",
        @"iPad14,1": @"iPad mini (6th gen)",
        @"iPad14,2": @"iPad mini (6th gen)",
        
        // iPod
        @"iPod1,1": @"iPod touch",
        @"iPod2,1": @"iPod touch (2nd gen)",
        @"iPod3,1": @"iPod touch (3rd gen)",
        @"iPod4,1": @"iPod touch (4th gen)",
        @"iPod5,1": @"iPod touch (5th gen)",
        @"iPod7,1": @"iPod touch (6th gen)",
        @"iPod9,1": @"iPod touch (7th gen)",
        
        // Simulator
        @"i386": @"Simulator",
        @"x86_64": @"Simulator",
        @"arm64": @"Simulator"
    };
    
    NSString *deviceName = deviceMapping[model];
    return deviceName ?: @"Unknown Device";
}

+ (DeviceFamily)deviceFamily {
    NSString *model = [self deviceModel];
    if ([model hasPrefix:@"iPhone"]) return DeviceFamilyiPhone;
    if ([model hasPrefix:@"iPad"]) return DeviceFamilyiPad;
    if ([model hasPrefix:@"iPod"]) return DeviceFamilyiPod;
    if ([model isEqualToString:@"i386"] || [model isEqualToString:@"x86_64"] || [model isEqualToString:@"arm64"])
        return DeviceFamilySimulator;
    return DeviceFamilyUnknown;
}

+ (NSString *)deviceResolution {
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;
    CGFloat scale = [[UIScreen mainScreen] scale];
    
    return [NSString stringWithFormat:@"%.0f x %.0f (@%.0fx)",
            screenWidth * scale,
            screenHeight * scale,
            scale];
}

+ (NSString *)deviceOSVersion {
    return [NSString stringWithFormat:@"iOS %@", [[UIDevice currentDevice] systemVersion]];
}

+ (NSString *)deviceIdentifier {
    return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}

+ (BOOL)isRetinaDisplay {
    return [UIScreen mainScreen].scale > 1.0;
}

+ (BOOL)isFaceIDCapable {
    if (@available(iOS 11.0, *)) {
        return [self isNotchDevice];
    }
    return NO;
}

+ (BOOL)isNotchDevice {
    if (@available(iOS 11.0, *)) {
        UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
        return window.safeAreaInsets.top > 20.0;
    }
    return NO;
}

+ (NSString *)deviceMarketingName {
    NSString *modelName = [self deviceModelName];
    
    // 为特定设备添加营销名称
    if ([modelName isEqualToString:@"iPhone10,3"] || [modelName isEqualToString:@"iPhone10,6"]) {
        return @"iPhone X";
    } else if ([modelName isEqualToString:@"iPhone11,2"]) {
        return @"iPhone XS";
    } else if ([modelName isEqualToString:@"iPhone11,4"] || [modelName isEqualToString:@"iPhone11,6"]) {
        return @"iPhone XS Max";
    } else if ([modelName isEqualToString:@"iPhone11,8"]) {
        return @"iPhone XR";
    } else if ([modelName isEqualToString:@"iPhone12,1"]) {
        return @"iPhone 11";
    } else if ([modelName isEqualToString:@"iPhone12,3"]) {
        return @"iPhone 11 Pro";
    } else if ([modelName isEqualToString:@"iPhone12,5"]) {
        return @"iPhone 11 Pro Max";
    } else if ([modelName isEqualToString:@"iPhone14,2"]) {
        return @"iPhone 13 Pro";
    } else if ([modelName isEqualToString:@"iPhone14,3"]) {
        return @"iPhone 13 Pro Max";
    } else if ([modelName isEqualToString:@"iPhone15,2"]) {
        return @"iPhone 14 Pro";
    } else if ([modelName isEqualToString:@"iPhone15,3"]) {
        return @"iPhone 14 Pro Max";
    } else if ([modelName isEqualToString:@"iPhone16,1"]) {
        return @"iPhone 15 Pro";
    } else if ([modelName isEqualToString:@"iPhone16,2"]) {
        return @"iPhone 15 Pro Max";
    }
    
    // 默认返回设备型号名称
    return modelName;
}

@end
